<template>
  <div class="user">
    <basic-container>
      <avue-crud
        ref="crud"
        v-model="form"
        :option="option"
        :page="page"
        :table-loading="listLoading"
        :before-open="handleOpenBefore"
        :data="list"
        @on-load="getList"
        @size-change="sizeChange"
        @current-change="currentChange"
        @search-change="handleFilter"
        @refresh-change="handleRefreshChange"
        @row-update="update"
        @row-save="create"
        @row-click="rowClick"
      >
        <template slot="menuLeft">
          <el-button
            @click="$refs.crud.rowAdd()"
          >新增
          </el-button>
           <el-button
            @click="topHandleUpdate"
          >编辑
          </el-button>
          <el-button
            @click="topDeletes"
          >删除
          </el-button>
          
        </template>

        <!-- <template
          slot="menu"
          slot-scope="scope"
        >
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,scope.index)"
          >编辑
          </el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="deletes(scope.row,scope.index)"
          >删除
          </el-button>
        </template> -->
      </avue-crud>
    </basic-container>
  </div>

</template>

<script>
// import { doGet, doPost, doDelet, doPut } from '@/router/axios'
import { formCreate } from "@/api/index";
// import { mapGetters } from 'vuex'
export default {
  name: 'TableUser',
  data() {
    return {
      option: tableOption,
      defaultProps: {
        label: 'name',
        value: 'id'
      },
      page: {
        total: 0, // 总页数
        current: 1, // 当前页数
        size: 20, // 每页显示多少条,
        isAsc: false,// 是否倒序
        // pageSizes: [1,2,3,10]
      },
      list: [],
      listLoading: true,
      form: {}
    }
  },
  computed: {

  },
  watch: {

  },
  created() {

  },
  methods: {
     rowClick(row,event,column) {
      //  this.selectionClear()
       console.log(row,event,column,'row,event,column')
       console.log(this.$refs.crud,'this.$refs.crud')
      // 点击表格行
      if (this.$refs.crud.$refs.table) {
        this.$refs.crud.$refs.table.clearSelection();
      }
      this.$refs.crud.$refs.table.toggleRowSelection(row, true);
    },
    // 上方操作
    topHandleUpdate(){
       let selection = this.$refs.crud.$refs.table.selection
       if (selection.length != 1) {
        return this.$message.warning("请选择一条数据");
      }
      console.log(selection[0],selection[0].$index)
       this.$refs.crud.rowEdit(selection[0], selection[0].$index)
    },
   
     topDeletes() {
        let selection = this.$refs.crud.$refs.table.selection
       if (selection.length != 1) {
        return this.$message.warning("请选择一条数据");
      }

      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', type: 'warning'
      }
      ).then(() => {
          formCreate.codeStrongelDete(selection[0].id).then((res) => {
          if (res.code == 200) {
            this.getList(this.page)
            this.$message.success('删除成功')
          } else {
            this.$message.error(res.msg)
          }
          loading()
        }).catch(() => {
          loading()
          this.$message.error('服务异常')
        })
        
      })
    },

    getList(page, params) {
      if (params == undefined || params == null || params == '') {
        params = {}
      }
      this.listLoading = false
      params.current = page.current - 1
      params.size = page.size
      // doGet('/basic/codeStrong/page', params).then(res => {
      //   this.list = res.data.data.dataList
      //   this.page.total = res.data.data.totalElements
      //   this.listLoading = false
      // })
      formCreate.codeStrongPage(params).then((res)=>{
        this.list = res.data.dataList
        this.page.total = res.data.totalElements
      })
    },
    sizeChange(size) {
      this.page.size = size
      this.page.current = 1
      this.getList(this.page)
    },
    currentChange(current) {
      this.page.current = current
      this.getList(this.page)
    },
    handleFilter(param, done) {
      this.page.current = 1
      this.getList(this.page, param)
      done()
    },
    handleRefreshChange() {
      this.getList(this.page)
    },
    handleOpenBefore(show, type) {
      window.boxType = type
      // 若是编辑、查看回显名称
      if (['edit', 'views'].includes(type)) {

      } else if (type === 'add') {

      }
      show()
    },
    handleUpdate(row, index) {
      this.$refs.crud.rowEdit(row, index)
    },
    create(row, done, loading) {
      // this.listLoading = true
      // doPost('/basic/codeStrong/save', this.form).then(res => {
      //   if (res.data.code == 200) {
      //     this.getList(this.page)
      //     done()
      //     this.$message.success('创建成功')
      //     return false
      //   } else {
      //     this.listLoading = false
      //     this.$message.error(res.data.msg)
      //   }
      //   loading()
      // }).catch(() => {
      //   loading()
      // })
      formCreate.codeStrongSave(this.form).then((res) => {
        if (res.code == 200) {
          this.getList(this.page)
          done()
          this.$message.success('创建成功')
          return false
        } else {
          // this.listLoading = false
          this.$message.error(res.msg)
        }
        loading()
      }).catch(() => {
        loading()
      })
    },
    update(row, index, done, loading) {
      // doPut('/basic/codeStrong/update', this.form).then(res => {
      //   if (res.data.code == 200) {
      //     this.getList(this.page)
      //     done()
      //     this.$message.success('更新成功')
      //     return
      //   } else {
      //     this.$message.error(res.data.msg)
      //     return
      //   }
      //   loading()
      // }).catch(() => {
      //   loading()
      // })
      formCreate.codeStrongUpdate(this.form).then((res) => {
        if (res.code == 200) {
          this.getList(this.page)
          done()
          this.$message.success('创建成功')
          return false
        } else {
          this.listLoading = false
          this.$message.error(res.msg)
        }
        loading()
      }).catch(() => {
        loading()
      })
    },
    deletes(row) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', type: 'warning'
      }
      ).then(() => {
        // doDelet('/basic/codeStrong/delete/' + row.id).then(res => {
        //   if (res.data.code == 200) {
        //     this.getList(this.page)
        //     this.$message.success('删除成功')
        //   } else {
        //     this.$message.error(res.data.msg)
        //   }
        //   loading()
        // }).catch(() => {
        //   loading()
        //   this.$message.error('服务异常')
        // })
          formCreate.codeStrongelDete(row.id).then((res) => {
          if (res.code == 200) {
            this.getList(this.page)
            this.$message.success('删除成功')
          } else {
            this.$message.error(res.msg)
          }
          loading()
        }).catch(() => {
          loading()
          this.$message.error('服务异常')
        })
        
      })
    }
  }
}

export const tableOption = {
  border: false,
  index: false,
  indexLabel: '序号',
  stripe: false,
  menuAlign: 'center',
  searchMenuSpan: 6,
  editBtn: false,
  delBtn: false,
  align: 'center',
  addBtn: false,
  selection: true,
  menu: false,
  column: [{
    fixed: true,
    label: 'id',
    prop: 'id',
    span: 24,
    hide: true,
    editDisplay: false,
    addDisplay: false
  },
  {
    fixed: true,
    label: '增强名称',
    prop: 'name',
    editDisplay: true,
    addDisplay: true,
    rules: [{
      required: true,
      trigger: 'blur',
      message: '请输入增强名称'
    },
    {
      min: '0',
      max: '50',
      message: '长度在 0 到 50 个字符',
      trigger: 'blur'
    }
    ],
    search: true,
    span: 12
  },
  {
    fixed: true,
    label: '增强编码',
    prop: 'code',
    editDisplay: true,
    addDisplay: true,
    rules: [{
      required: true,
      trigger: 'blur',
      message: '请输入增强编码'
    },
    {
      min: '0',
      max: '20',
      message: '长度在 0 到 20 个字符',
      trigger: 'blur'
    }
    ],
    search: true,
    span: 12
  },
  {
    fixed: true,
    label: '类名',
    prop: 'strongClass',
    editDisplay: true,
    addDisplay: true,
    rules: [{
      required: true,
      trigger: 'blur',
      message: '请输入增强类名'
    },
    {
      min: '0',
      max: '50',
      message: '长度在 0 到 50 个字符',
      trigger: 'blur'
    }
    ],
    span: 12
  },
  {
    fixed: true,
    label: '方法名',
    prop: 'strongMethod',
    editDisplay: true,
    addDisplay: true,
    rules: [{
      required: true,
      trigger: 'blur',
      message: '请输入增强方法名'
    },
    {
      min: '0',
      max: '25',
      message: '长度在 0 到 25 个字符',
      trigger: 'blur'
    }
    ],
    span: 12
  },
  {
    fixed: true,
    label: '备注',
    prop: 'remarks',
    type: 'textarea',
    editDisplay: true,
    addDisplay: true,
    rules: [{
      required: true,
      trigger: 'blur',
      message: '请输入备注'
    },
    {
      min: '0',
      max: '255',
      message: '长度在 0 到 255 个字符',
      trigger: 'blur'
    }
    ],
    span: 24
  },
  {
    fixed: true,
    label: '创建时间',
    prop: 'createTime',
    editDisplay: false,
    addDisplay: false,
    span: 12
  },
  {
    fixed: true,
    label: '更新时间',
    prop: 'updateTime',
    editDisplay: false,
    addDisplay: false,
    span: 12
  }
  ]
}

</script>
<style lang="scss" scoped>
/deep/.avue-crud__right {
  /deep/.el-button{
    min-width: 10px;
  }
}

</style>
